package com.zjdiepu.www.biz.dh;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.dao.dh.DhLoanAfterMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.dh.IDhLoanAfterService;
import com.zjdiepu.www.model.dh.dto.DhLoanAfter;
import com.zjdiepu.www.model.dh.ext.DhLoanAfterExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 贷后信息登记Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月10日 14时56分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhLoanAfterServiceImpl extends BaseService implements IDhLoanAfterService {
	public static final Logger logger = LoggerFactory.getLogger(DhLoanAfterServiceImpl.class);

	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private DhLoanAfterMapper dhLoanAfterMapper;
	@Autowired
	private YwBusinessLoanMapper ywBusinessLoanMapper;
	
	@Override
	public ApiResponse saveLoanAfter(DhLoanAfter dhLoanAfter,
			AbstractSysUser currentUser) {
		String id = dhLoanAfter.getId();
		if(StringUtils.isBlank(id)) {
			dhLoanAfter.initInsertData(currentUser.getId(), currentUser.getName());
			dhLoanAfterMapper.insert(dhLoanAfter);
		} else {
			dhLoanAfter.initUpdateData(currentUser.getId(), currentUser.getName());
			dhLoanAfterMapper.updateByPrimaryKeySelective(dhLoanAfter);
		}
		Date dateFirstMonth = dhLoanAfter.getDateFirstMonth();
		if(null != dateFirstMonth) {
			String busiId = dhLoanAfter.getBusiId();
			if(StringUtils.isBlank(busiId)) {
				ExceptionHelper.thow("999","业务ID为空");
			}
			YwBusinessLoan ywBusinessLoan = new YwBusinessLoan();
			ywBusinessLoan.setBusiId(busiId);
			ywBusinessLoan.setDpStatus(DpStatus.NORMAL.getCode());
			ywBusinessLoan = ywBusinessLoanMapper.selectRefined(ywBusinessLoan);
			if(null == ywBusinessLoan) {
				ExceptionHelper.thow("999","贷款记录丢失或已被删除");
			}
			Integer loanMonth = ywBusinessLoan.getLoanMonth();
			if(null == loanMonth) {
				ExceptionHelper.thow("999","贷款期限为空");
			}
			YwBusiness ywBusiness = new YwBusiness();
			ywBusiness.setId(busiId);
			ywBusiness.setDateRepaymentLast(DateUtil.addMonths(dateFirstMonth, loanMonth));
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		}
		return ApiResponse.success(dhLoanAfter.getId());
	}
	
	@Override
	public void saveUpdateAndDelete(DhLoanAfter dhLoanAfter, OperateType operType) {
		switch (operType) {
		case add:
			dhLoanAfter.initInsertData(dhLoanAfter.getModifier(), dhLoanAfter.getModifierName());
			dhLoanAfterMapper.insert(dhLoanAfter);
			break;
		case edit:
			dhLoanAfter.initUpdateData(dhLoanAfter.getModifier(), dhLoanAfter.getModifierName());
			// 影响的记录数
			int updateCount = dhLoanAfterMapper.updateByPrimaryKeySelective(dhLoanAfter);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhLoanAfter.initDeleteData(dhLoanAfter.getModifier(), dhLoanAfter.getModifierName());
			// 影响的记录数
			int deleteCount = dhLoanAfterMapper.updateByPrimaryKeySelective(dhLoanAfter);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhLoanAfter> selectByExt(DhLoanAfterExt dhLoanAfterExt) {
		return dhLoanAfterMapper.selectByExt(dhLoanAfterExt);
	}
	
	@Override
	public int countByExt(DhLoanAfterExt dhLoanAfterExt){
		return dhLoanAfterMapper.countByExt(dhLoanAfterExt);
	}
	
	@Override
	public DhLoanAfter selectById(String id) {
		return dhLoanAfterMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhLoanAfterMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhLoanAfter dhLoanAfter) {
		return dhLoanAfterMapper.countRefined(dhLoanAfter);
	}

	@Override
	public DhLoanAfter selectRefined(DhLoanAfter dhLoanAfter) {
		return dhLoanAfterMapper.selectRefined(dhLoanAfter);
	}
	
	@Override
	public int deletedRefined(DhLoanAfter dhLoanAfter) {
		return dhLoanAfterMapper.deletedRefined(dhLoanAfter);
	}

	@Override
	public boolean repeat(DhLoanAfter dhLoanAfter) {
		int count = countRefined(dhLoanAfter);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhLoanAfter.getId())) {
			return true;
		}
		DhLoanAfter dbItem = selectRefined(dhLoanAfter);
		if(null == dbItem) {
			return false;
		}
		return !dhLoanAfter.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhLoanAfterMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DhLoanAfter> dhLoanAfters) {
		return dhLoanAfterMapper.insertBatch(dhLoanAfters);
	}
}
